/*This file contains the menulist adapter, 
 * it's the link between the interface of the list 
 * and the list of the contacts' name. */
package dk.aau.walkietalkieui.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import dk.aau.walkietalkie.database.DBController;
import dk.aau.walkietalkie.database.Identification;
import dk.aau.walkietalkie.network.UDPBroadcastSender;
import dk.aau.walkietalkieui.ContactListActivity;
import dk.aau.walkietalkieui.R;

public class MenuListAdapter extends BaseAdapter {

	// TODO deal with online and offline contact
	// list of users ArrayList<Identification>
	// private String[] contactList = new String[]{
	// "Broadcast",
	// "Guillaume",
	// "John",
	// "Caroline",
	// "Katina",
	// "Goncalo",
	// "Carlos",
	// "He"
	// };
	private ArrayList<Identification> contactList = new ArrayList<Identification>();
	// data to add

	public static final int NO_ITEM_SELECTED = -1;
	private Context ctx;
	private int selectedPosition;

	public MenuListAdapter(Context ctx) {
		this.ctx = ctx;
		this.selectedPosition = NO_ITEM_SELECTED;
		contactList.add(new Identification(UDPBroadcastSender.GENERIC_BROADCAST_ADDRESS, "Broadcast", 0));
	}

	@Override
	public int getCount() {
		return contactList.size();
	}

	@Override
	public Identification getItem(int position) {
		return contactList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v;
		if (convertView == null) {
			v = View.inflate(ctx, R.layout.contact_list_row, null);
		} else {
			v = convertView;
		}

		// This is the name of the contacts
		((TextView) v.findViewById(R.id.Text)).setText(getItem(position)
				.getName());
		((TextView) v.findViewById(R.id.TextDetail)).setText(getItem(position)
				.getIp());

		// This image is the small image in the list_row
		ImageView image = (ImageView) v.findViewById(R.id.Image);

		if (position == getCount()-1) {
			image.setImageDrawable(ctx.getResources().getDrawable(
					R.drawable.broadcast));
		} else {
			image.setImageDrawable(ctx.getResources().getDrawable(
					R.drawable.online));
		}

		// This displays the list
		ViewGroup parentLayout = (ViewGroup) v.findViewById(R.id.Parent);
		if (selectedPosition == position) {
			parentLayout.setBackgroundColor(ctx.getResources().getColor(
					R.color.blue));
		} else {
			parentLayout.setBackgroundColor(ctx.getResources().getColor(
					R.color.none));
		}
		return v;
	}

	public void setSelectedItem(int position) {
		this.selectedPosition = position;
		notifyDataSetChanged();
	}

	public void updateList() {
		DBController controller = new DBController(ctx);
		contactList = controller.getAllContacts();
		contactList.add(new Identification(UDPBroadcastSender.GENERIC_BROADCAST_ADDRESS, "Broadcast", 0));
		controller.close();
	}

}
